﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Math.Compiler;
using System;
using Math.Compiler.Expressions;
using System.Linq.Expressions;

namespace Math.Compiler.Classes
{
    public class Variable
    {
#if DEBUG
        private string _inputReferencesText;
        private string _outputReferencesText;
#endif
        private int _id;

        private static Dictionary<string, Variable> _variables;

        static Variable()
        {
            _variables = new Dictionary<string, Variable>();
        }

        private bool _visited = false;

        private double _value;

        private MathExpression _expression;

        private readonly string _name;

        private Variable(int id, string name)
        {
            _id = id;
            _name = name;
            _inputReferences = new HashSet<Variable>();
            _outputReferences = new HashSet<Variable>();
        }

        public int Id
        {
            get
            {
                return _id;
            }
        }

        private static int _count = 0;

        public static Variable Load(string name)
        {
            if (!_variables.ContainsKey(name))
            {
                Variable variable = new Variable(_count++, name);
                _variables.Add(name, variable);
                return variable;
            }
            return _variables[name];
        }

        public bool HasExpression
        {
            get
            {
                return _expression != null;
            }
        }

        public void Eval()
        {
            if (_visited == false)
            {
                _visited = true;
                if (_expression != null)
                {
                    //Func<double> f = _expression;
                    //_value = f();
                    _value = _expression.Eval();
                }
                else
                {
                    _value = 0;
                }
                if (_outputReferences.Count > 0)
                {
                    foreach (Variable cell in _outputReferences)
                    {
                        cell.Eval();
                    }
                }
            }
        }

        public void Visit()
        {
            if (_visited == true)
            {
                _visited = false;
                if (_inputReferences.Count > 0)
                {
                    foreach (var cell in _inputReferences)
                    {
                        cell.Visit();
                    }
                }
                Eval();
            }
        }

        public double Double
        {
            get
            {
                return _value;
            }
        }

        public MathExpression MathExpression
        {
            get
            {
                return _expression;
            }
            set
            {
                if (_expression != value)
                {
                    _expression = value;
                    _visited = false;
                    Eval();
                }
            }
        }

        public string Value
        {
            get
            {
                if (_expression != null)
                {
                    return _value.ToString(CultureInfo.InvariantCulture.NumberFormat);
                }
                return string.Empty;
            }
        }

        private string _text;

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

        private readonly HashSet<Variable> _outputReferences;

        private readonly HashSet<Variable> _inputReferences;

        private void Subscribe(Variable cell)
        {
            cell.SubscribeInput(this);
            SubscribeOutput(cell);
        }

        private void Unsubscribe(Variable cell)
        {
            cell.UnsubscribeInput(this);
            UnsubscribeOutput(cell);
        }

        private void SubscribeInput(Variable cell)
        {
#if DEBUG
            _inputReferencesText = null;
#endif
            _inputReferences.Add(cell);
        }

        private void UnsubscribeInput(Variable cell)
        {
#if DEBUG
            _inputReferencesText = null;
#endif
            _inputReferences.Remove(cell);
        }

        private void SubscribeOutput(Variable cell)
        {
#if DEBUG
            _outputReferencesText = null;
#endif
            _outputReferences.Add(cell);
        }

        private void UnsubscribeOutput(Variable cell)
        {
#if DEBUG
            _outputReferencesText = null;
#endif
            _outputReferences.Remove(cell);
        }

        public void Clear()
        {
#if DEBUG
            _inputReferencesText = null;
            _outputReferencesText = null;
#endif
            _inputReferences.Clear();
            _outputReferences.Clear();
            _expression = null;
            _text = null;
            _value = 0;
        }

        public void Subscribe()
        {
            HashSet<Variable> outputReferences = new HashSet<Variable>();
            Decompile(outputReferences, MathExpression);
            foreach (Variable cell in outputReferences)
            {
                cell.SubscribeInput(this);
                this.SubscribeOutput(cell);
            }
        }

        public void Unsubscribe()
        {
            HashSet<Variable> outputReferences = new HashSet<Variable>();
            Decompile(outputReferences, MathExpression);
            foreach (Variable cell in outputReferences)
            {
                cell.UnsubscribeInput(this);
                this.UnsubscribeOutput(cell);
            }
        }

#if DEBUG
        public override string ToString()
        {
            if (_inputReferencesText == null)
            {
                _inputReferencesText = GetReferences(_inputReferences, _inputReferences.Count);
            }
            if (_outputReferencesText == null)
            {
                _outputReferencesText = GetReferences(_outputReferences, _outputReferences.Count);
            }
            bool inputIsNull = string.IsNullOrEmpty(_inputReferencesText);
            bool outputIsNull = string.IsNullOrEmpty(_outputReferencesText);
            if (!inputIsNull && !outputIsNull)
            {
                return string.Format("{0}->{1}->{2}", _outputReferencesText, _name, _inputReferencesText);
            }
            if (!inputIsNull)
            {
                return string.Format("{0}->{1}", _name, _inputReferencesText);
            }
            if (!outputIsNull)
            {
                return string.Format("{0}->{1}", _outputReferencesText, _name);
            }
            return _name;
        }
#else
        public override string ToString()
        {
            return _name;
        }
#endif

        private string GetReferences(IEnumerable<Variable> cells, int count)
        {
            IEnumerator<Variable> enumerator = cells.GetEnumerator();
            StringBuilder sb = new StringBuilder();
            if (enumerator.MoveNext())
            {
                Variable cell = enumerator.Current;
                sb.Append(cell.Name);
                int index = 0;
                while (enumerator.MoveNext())
                {
                    if (index == 0 && index < count - 2)
                    {
                        sb.Append(",...");
                        index++;
                        continue;
                    }
                    if (index > 0 && index < count - 2)
                    {
                        index++;
                        continue;
                    }
                    cell = enumerator.Current;
                    sb.Append(',');
                    sb.Append(cell.Name);
                    index++;
                }
            }
            return sb.ToString();
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public bool Validate(MathExpression expression)
        {
            foreach (Variable cell in Decompile(expression))
            {
                if (cell == this)
                {
                    return false;
                }
            }
            return true;
        }

        private HashSet<Variable> Decompile(MathExpression expression)
        {
            HashSet<Variable> cells = new HashSet<Variable>();
            Decompile(cells, expression);
            return cells;
        }

        public void Decompile(HashSet<Variable> cells, MathExpression expression)
        {
            if (expression != null)
            {
                Stack<MathExpression> expressions = new Stack<MathExpression>();
                expressions.Push(expression);
                do
                {
                    MathExpression current = expressions.Pop();
                    if (current.Children != null)
                    {
                        foreach (MathExpression node in current.Children)
                        {
                            VariableReference reference = node as VariableReference;
                            if (reference != null)
                            {
                                if (cells.Add(reference.Variable))
                                {
                                    if (reference.Variable.MathExpression != null)
                                    {
                                        expressions.Push(reference.Variable.MathExpression);
                                    }
                                }
                            }
                            else
                            {
                                if (node != null)
                                {
                                    expressions.Push(node);
                                }
                            }
                        }
                    }
                    VariableReference referenced = current as VariableReference;
                    if (referenced != null)
                    {
                        if (cells.Add(referenced.Variable))
                        {
                            if (referenced.Variable.MathExpression != null)
                            {
                                expressions.Push(referenced.Variable.MathExpression);
                            }
                        }
                    }
                }
                while (expressions.Count > 0);
            }
        }

        private string _updateText;
        private MathExpression _updateExpression;

        public void BeginUpdate()
        {
            _updateText = _text;
            _updateExpression = _expression;
        }

        public void EndUpdate()
        {
            _updateText = null;
            _updateExpression = null;
        }

        public void CancelUpdate()
        {
            _text = _updateText;
            _expression = _updateExpression;
        }
    }
}
